import {defHttp} from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";
const { createMessage: $message } = useMessage();

enum Api {
  treeGrid = '/cooperator/treeGrid',

  get = '/cooperator/queryById',
  save='/cooperator/add',
  edit='/cooperator/edit',
  delete = '/cooperator/delete',

  asyncZtreeNode = '/cooperator/asyncZtreeNode',
  updateBatchOrders = '/cooperator/updateBatchOrders',
}

/**
 * 列表接口
 * @param params
 */
export const treeGrid = async (params) => await defHttp.get({url: Api.treeGrid, params});

/**
 * 查询详情
 * @param params
 */
export const getCooperatorById = (params) => {
  return defHttp.get({ url: Api.get, params });
}

/**
 * 保存或者更新菜单
 * @param params
 */
export const cooperatorSave = (url, params) => {
  return defHttp.post({url: url, params});
}

/**
 * 删除菜单
 */
export const cooperatorDelete = (params,handleSuccess) => {
  return defHttp.delete({
    url: Api.delete,
    params
  }, {joinParamsToUrl: true, isTransformResponse: false}).then((res) => {
    if (res.success) {
      $message.success(res.message)
      handleSuccess()
    } else {
      $message.error(res.message)
    }
  });
}

/**
 * BasticTree
 */
export const asyncZtreeNode = async (params) => {
  let result = await defHttp.get({url: Api.asyncZtreeNode, params});
  return result.map(item => {
    if (item.hasChildren === 'true') {
      return { ...item, children: [] }
    } else {
      return item
    }
  })
}

/**
 * 查询详情
 * @param params
 */
export const updateBatchOrders = () => {
  return defHttp.get({ url: Api.updateBatchOrders });
}
